<template>
  <select-tree :list="userList" v-model="user" v-on="$listeners" v-bind="$attrs" filterable :option="option"  :disable-branch-nodes="true"></select-tree>
</template>
<script>
  import user from "@/api/systemManagement/user/index";
  import SelectTree from './SelectTree'
  import {handleTree,concatChildren} from "@/utils/common";
  export default {
    name:"UserAllSelect",
    components: { SelectTree},
    props:{
      value:{
        type:[String,Number,Array],
        default:undefined
      },
      keyname:{//有时候要传username,有时候要传id
        type:String,
        default:undefined
      },
      params:{
        type:Object,
        default:()=>{return {d:'all',r:'all'}}
      }
    },
    data(){
      return {
        userList:[],
        option:{id:"id",name:"alias",children:"users"},
      }
    },
    created() {
      if(this.keyname){
        this.option.id = this.keyname
      }
      this.getAccountsAllUsers()
    },
    computed: {
      user: {
        get() {
          return this.value
        },
        set(v) {
          this.$emit('input', v)
        }
      }
    },
    methods:{
      getAccountsAllUsers(data) {
        user.getAccountsAllUsers(this.params).then(res => {
          let list = handleTree(res.data, "id", "parent_dept");
           concatChildren(list,this.keyname)
           this.userList = list;
        });
      },
    }
  }
</script>
